<template>
    <el-breadcrumb separator="/" id="breadcrumb">
        <el-breadcrumb-item :to="{ path: '/home/system' }">系统设置</el-breadcrumb-item>
        <el-breadcrumb-item>菜单资源</el-breadcrumb-item>
    </el-breadcrumb>
    <tableList
            :dict="dict"
            :tableData="tableData"
            :tableConfig="tableConfig"
            @doit="tableDoit"
    />
    <drawer
            :dict="dict"
            :drawerData="drawerData"
            :drawerConfig="drawerConfig"
            @doit="drawerDoit"
    />
    <deleteDialog
        :deleteDialogConfig="deleteDialogConfig"

    />
</template>

<script setup lang="ts">
import tableList from '@/components/TableList.vue'
import drawer from '@/components/Drawer.vue'
import deleteDialog from '@/components/DeleteDialog.vue'
import {ref, reactive, onMounted} from 'vue'
import {addMenu, delMenu, editMenu, getMenuList} from "@/api/system/menu.ts";
import {createDict} from "@/common/dict.ts";
import {ElMessage} from 'element-plus'

//table
import {table_config} from './tableConfig.js'

let tableConfig:any = reactive(table_config()) //表格配置
let tableData:any = ref([]) //表格数据
let search:any = reactive({})
//drawer
import {drawer_config} from './drawerConfig.js'

let drawerConfig:any = reactive(drawer_config()) //抽屉配置
const thing:any = { //新建的实体
    "remark": "",
    "menuId": "",
    "menuName": "",
    "parentName": "",
    "parentId": "",
    "orderNum": 1,
    "path": "",
    "component": "",
    "query": "",
    "isFrame": "1",
    "isCache": "0",
    "menuType": "M",
    "visible": "0",
    "status": "0",
    "perms": "",
    "icon": "",
    "children": []
}
let drawerData:any = ref({})
let deleteDialogConfig:any = ref({
    visible: false,
    message: "是否删除",
    alertTitle:"警告",
    confirmText: "确认",
    cancelText: "取消",
    confirmFunction: () => {
    },
    cancelFunction: () => {
    }
}) //删除对话框配置
const dictKey = ['sys_normal_disable', 'sys_menu_type'] //本页面所需要的字典值
let dict = reactive({})

onMounted(async () => {
    dict = await createDict(dictKey) //模块字典值初始化
    await dataInit() //数据初始化
})

//数据初始化
async function dataInit() {
    //数据初始化
    const r = await getMenuList(search)
    tableData.value = r.data
    tableConfig.total = r.total
    //格式化添加修改的表单数据
    drawerConfig.form[1].options = tableData.value
}

//table事件调度中心
async function tableDoit(e) {
    const eventName = e.eventName, val = e.value
    // @ts-ignore
    tell.log('表格事件分析', eventName, val)
    switch (eventName) {
        case 'refresh': //数据重新加载
            await dataInit()
            break;
        case 'expand': //展开或回收列表
            tableConfig.defaultExpandAll = !tableConfig.defaultExpandAll
            break;
        case 'add': //新增数据
            drawerData.value = Object.assign({}, thing)
            drawerConfig.show = true
            break;
        case 'addChildren': //新增子节点
            drawerData.value = Object.assign({}, thing)
            drawerData.value['parentId'] = val.menuId
            drawerConfig.show = true
            break;
        case 'edit': //编辑数据
            let tmpVal = Object.assign({}, val) //深度复制，防止污染
            delete tmpVal.value
            delete tmpVal.label
            delete tmpVal.children
            drawerData.value = tmpVal
            drawerConfig.show = true
            break;
        case 'delete': //删除
            deleteDialogConfig.value = {
                visible: true,
                message: "是否删除，删除后无法恢复数据。请再次确认！",
                alertTitle:"警告",
                confirmText: "确认",
                cancelText: "取消",
                confirmFunction: async () => {
                    let result = await delMenu(val.menuId)
                    if (result.code == 200){
                      await dataInit()
                        ElMessage({
                            message: '删除成功，用户功能待用户刷新后才能正常使用.',
                            type: 'success',
                        })
                        deleteDialogConfig.value.visible = false
                    }else{
                        ElMessage({
                            message: '数据错误，请管理员查看问题',
                            type: 'warning',
                        })
                    }
                },
            }
            break;
        case 'search': //搜索
            search = val
          await dataInit()
            break;
        default:
          // @ts-ignore
            tell.log('未知组件事件', eventName, val)
    }
}

//drawer事件调度中心
async function drawerDoit(e) {
    const eventName = e.eventName, val = e.value
  // @ts-ignore
    tell.log('抽屉事件分析', eventName, val)
    switch (eventName) {
        case 'save': //保存数据
            let result:any = ""
            if (val.menuId){
                result = await editMenu(val)
            }else{
                result = await addMenu(val)
            }

            if (result.code == 200){
                await dataInit()
                ElMessage({
                    message: '新增成功',
                    type: 'success',
                })
                drawerConfig.show = false
            }else{
                ElMessage({
                    message: '数据错误，请管理员查看问题',
                    type: 'warning',
                })
            }

            break;
        case 'close': //关闭弹窗
            drawerConfig.show = false
            break;
        default:
          // @ts-ignore
            tell.log('未知组件事件', eventName, val)
    }
}
</script>
<style scoped lang="scss">
.content {
  padding-top: 20px;
}
</style>